<?php
namespace Api\Controller;
use Think\Controller;
class UserController extends CommonController{

        public function register() {

            $code = I('post.code');
            $mobile = I('post.mobile');

            $mobileCodeMap['mobile'] = $mobile;

            $mobileCode = $this->mobileCodeModel->where($mobileCodeMap)->find();

            //验证短信验证码是否一致
            if(isNullOrEmpty($mobileCode) || $code != $mobileCode['code']) {
                $this->errorReturn('ERROR_MOBILE_CODE');
            }
            //删除手机验证码记录
            $this->mobileCodeModel->where($mobileCodeMap)->delete();

            $userMap['mobile'] = $mobile;

            $user = $this->userModel
                ->where($userMap)
                ->find();

            //验证用户是否存在
            if(!isNullOrEmpty($user)) {
                $this->errorReturn('ERROR_USER_EXIST');
            }

            $userData["create_time"] = $this->getCurrentTime();
            $userData["mobile"] = $mobile;
            $userData["password"] = md5(I("post.password"));
            $userData["token"] = createUuid();

            $this->userModel->add($userData);

            $this->returnData['data']['token'] = $userData["token"];

            $this->response($this->returnData, 'json');

        }

        public function login() {

            $mobile = I("post.mobile");

            $user = $this->userModel->queryByMobile($mobile);

            if(isNullOrEmpty($user)) {
                $this->errorReturn("ERROR_LOGIN");
            }

            //判断密码
            $password = I("post.password");

            if(md5($password) != $user["password"]) {
                $this->errorReturn("ERROR_LOGIN");
            }

            $this->returnData['data']['token'] = $user["token"];

            $this->response($this->returnData, 'json');

        }

        public function masterInfo() {

            //判断用户是否登录
            $token = I("post.token");

            $currentUser = $this->userModel->queryByToken($token);

            if(isNullOrEmpty($currentUser)) {
                $this->errorReturn("ERROR_TOKEN");
            }

            //判断师傅是否存在
            $masterId = I("post.masterId");

            $master = $this->userModel->queryById($masterId);

            if(isNullOrEmpty($master)) {
                $this->errorReturn("ERROR_MASTER_NOT_FOUND");
            }

            $basicInfo = $this->userModel->getMasterDetailInfo($currentUser["user_id"], $masterId);
            $commentInfo = $this->commentModel->getMasterPageInfo($masterId);

            $this->returnData['data']['basic_info'] = $basicInfo;
            $this->returnData['data']['comment_info'] = $commentInfo;

            $this->response($this->returnData, 'json');

        }

}